package com.system.Controller;

import com.common.Entity.Result;
import com.system.Service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// 验证码控制器
@RestController
@RequestMapping("/verification")
@Slf4j
public class VerificationCodeController {
    @Autowired
    private VerificationCodeService verificationCodeService;

    @GetMapping("/send-email-code")
    public Result<Void> sendEmailCode(@RequestParam(required = true) String email) {
        boolean success = verificationCodeService.sendEmailCode(email);
        return success ? Result.success() : Result.error("验证码发送失败");
    }

    @GetMapping("/send-phone-code")
    public Result<Void> sendPhoneCode(@RequestParam String phone) {
        boolean success = verificationCodeService.sendPhoneCode(phone);
        return success ? Result.success() : Result.error("验证码发送失败");
    }

    @PostMapping("/verify")
    public Result<Void> verifyCode(
            @RequestParam String type,
            @RequestParam String receiver,
            @RequestParam String code) {
        boolean success = verificationCodeService.verifyCode(type, receiver, code);
        return success ? Result.success() : Result.error("验证码错误或已过期");
    }
}